/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.core.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.tool.DateTime;
import org.springblade.common.view.PairView;
import org.springblade.core.log.entity.LogAudit;
import org.springblade.core.log.vo.LogAuditVO;
import org.springblade.core.log.mapper.LogAuditMapper;
import org.springblade.core.log.service.ILogAuditService;
import org.springblade.core.log.wrapper.LogAuditWrapper;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.system.cache.SysCache;
import org.springblade.system.feign.ISysClient;
import org.springblade.system.user.feign.IUserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 服务实现类
 * @author Blade
 * @since 2022-08-28
 */
@Service
public class LogAuditServiceImpl extends BaseServiceImpl<LogAuditMapper, LogAudit> implements ILogAuditService {

	private static AtomicInteger count = new AtomicInteger(0);

	@Autowired
	private IUserClient userClient;

	@Autowired
	private ISysClient sysClient;

	@Override
	public IPage<LogAuditVO> selectLogAuditPage(IPage<LogAudit> page, LogAuditVO logAudit) {

		String realName = logAudit.getRealName();
		List<Long> userIds = null;
		if (StringUtil.isNotBlank(realName)) {
			userIds = userClient.getUserIds(realName);
		}

		boolean notTimeEmpty = ObjectUtil.isNotEmpty(logAudit.getCreateTime());
		String time = null;
		if (notTimeEmpty) {
			time = DateTime.simpleDateFormat.format(logAudit.getCreateTime()).substring(0, 13);
		}

		Integer functionType = logAudit.getFunctionType();
		Wrapper<LogAudit> queryWrapper = new QueryWrapper<LogAudit>()
			.eq(ObjectUtil.isNotEmpty(functionType), "function_type", functionType)
			.in(ObjectUtil.isNotEmpty(userIds), "user_id", userIds)
			.like(notTimeEmpty, "create_time", time);
		IPage<LogAudit> logAuditIPage = this.page(page, queryWrapper);
		return LogAuditWrapper.build().pageVO(logAuditIPage);
	}

	/**
	 * 匹配操作并存储
	 * @param logAudit
	 */
	@Override
	public void matchingOrSave(LogAudit logAudit) {
		PairView<Integer> pairView = sysClient.getFunctionPair(logAudit.getPath());
		log.debug("pairView is :" + pairView);
		logAudit.setFunctionName(pairView.getName());
		logAudit.setFunctionType(pairView.getValue());
		this.save(logAudit);
	}

}
